import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 东莞呵呵
 * Date:2022-08-26
 * Time:21:42
 */
public class JDBCSelect {
    public static void main(String[] args) throws SQLException {
        //1、创建数据源
        DataSource dataSource=new MysqlDataSource();
        ((MysqlDataSource)dataSource).setURL("jdbc:mysql://127.0.0.1:3306/test?characterEncoding=utf8&useSSL=false");
        ((MysqlDataSource)dataSource).setUser("root");
        ((MysqlDataSource)dataSource).setPassword("123456");

        //2、建立连接
        Connection connection=dataSource.getConnection();


        //3、构造sql语句
        String sql="select * from student";
        PreparedStatement statement= connection.prepareStatement(sql);

        //4、执行sql语句
        ResultSet resultSet = statement.executeQuery();

        //5、打印数据表
        while(resultSet.next()){
            //"id"为列名，也可以使用序号（从1开始），一般最好使用名字（可读性更好）
            int id=resultSet.getInt("id");
            String name=resultSet.getString("name");
            System.out.println("id: "+id+"; "+"name: "+name+"; ");
        }



        //5、关闭资源
        resultSet.close();
        statement.close();
        connection.close();
    }
}
